﻿using SkypeKit;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ISIM
{
    public partial class SkypeContactSearch : Form
    {
        private SktSkype skype;

        private class MyContact
        {
            public SktContact contact;

            public MyContact(SktContact contact)
            {
                this.contact = contact;
            }

            // Prettying up the string to display in our listboxes..
            public override string ToString()
            {
                string authStatus = "";
                if (contact.P_GIVEN_AUTHLEVEL == SktContact.AUTHLEVEL.AUTHORIZED_BY_ME) authStatus = " (" + Global.LangManager.Translate("alreadyInContactList") + ")";
                if (contact.P_GIVEN_AUTHLEVEL == SktContact.AUTHLEVEL.BLOCKED_BY_ME) authStatus = " (" + Global.LangManager.Translate("blocked") + ")";
                return contact.P_SKYPENAME + " " + contact.P_FULLNAME + " " + contact.P_CITY + authStatus;
            }
        }

        public SkypeContactSearch(SktSkype skype)
        {
            InitializeComponent();

            label2.Text = Global.LangManager.Translate("fullName");
            quickAddButton.Text = Global.LangManager.Translate("quickAdd");
            search.Text = Global.LangManager.Translate("search");

            this.skype = skype;

            skype.events.OnContactSearchContactSearchStatus += OnContactSearchContactSearchStatus;
            skype.events.OnContactSearchNewResult += OnContactSearchNewResult;

            contactSearchResultLB.HorizontalScrollbar = true;
        }

        // This callback gets fired every time a new matching contact is found
        public void OnContactSearchNewResult(SktContactSearch sender, SktEvents.OnContactSearchNewResultArgs e)
        {
            if (InvokeRequired)
            {
                Invoke(new MethodInvoker(() => OnContactSearchNewResult(sender, e)));
                return;
            }

            contactSearchResultLB.Items.Add(new MyContact(e.contact));
        }

        // This callback could be useful for checking if e.value == SktContactSearch.STATUS.FINISHED
        public void OnContactSearchContactSearchStatus(SktContactSearch sender, SktEvents.OnContactSearchContactSearchStatusArgs e)
        {
            if (InvokeRequired)
            {
                Invoke(new MethodInvoker(() => OnContactSearchContactSearchStatus(sender, e)));
                return;
            }

            if ((e.value == SktContactSearch.STATUS.FINISHED) || (e.value == SktContactSearch.STATUS.FAILED) || (e.value == SktContactSearch.STATUS.EXTENDABLE))
            {
                sender.Release();
            }
        }

        // Using skype.CreateBasicContactSearch method. This method takes a string argument and looks for non-exact matches 
        // against both P_SKYPENAME and P_FULLNAME properties of the contact. If you intend to implement a simple, 
        // one-input search feature - this is the best method for you. The non-exact matching operates similarly to the SQL LIKE condition. 

        private void BasicContactSearch()
        {
            SktContactSearch seeker = skype.CreateBasicContactSearch(skypeNameTextBox.Text);
            if (!seeker.IsValid())
            {
                DialogResult result = MessageBox.Show(this, Global.LangManager.Translate("searchingError"), Global.LangManager.Translate("error"),
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            seeker.Submit();
        }

        // Creating a blank contact search object and then adding custom terms. For more information about composing
        // complex search objects, see SktContactSearch section in the reference manual.

        private void ComplexContactSearch()
        {
            SktContactSearch seeker = skype.CreateContactSearch();

            if (skypeNameTextBox.Text.Replace(" ", "").Length > 0)
                seeker.AddStrTerm((int)SktContact.PropKeys.P_SKYPENAME, SktContactSearch.CONDITION.CONTAINS_WORDS, skypeNameTextBox.Text);
            if (fullNameTextBox.Text.Replace(" ", "").Length > 0)
                seeker.AddStrTerm((int)SktContact.PropKeys.P_FULLNAME, SktContactSearch.CONDITION.CONTAINS_WORDS, fullNameTextBox.Text);
            if (emailTextBox.Text.Replace(" ", "").Length > 0)
                seeker.AddEmailTerm(emailTextBox.Text);

            if (!seeker.IsValid())
            {
                DialogResult result = MessageBox.Show(this, Global.LangManager.Translate("searchingError"), Global.LangManager.Translate("error"),
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            seeker.Submit();
        }

        private void search_Click(object sender, EventArgs e)
        {
            contactSearchResultLB.Items.Clear();
            if (skypeNameTextBox.Text.Equals("") & fullNameTextBox.Text.Equals("") & emailTextBox.Text.Equals("")) return; // all fields are empty
            if (fullNameTextBox.Text.Equals("") & emailTextBox.Text.Equals("")) { BasicContactSearch(); return; }; // only skypename field is used
            ComplexContactSearch(); // multiple fields were used
        }

        private void SearchFieldKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) search_Click(sender, e);
        }

        private void contactSearchResultLB_DoubleClick(object sender, EventArgs e)
        {
            if (contactSearchResultLB.Items.Count == 0) return;

            MyContact contact = (MyContact)contactSearchResultLB.Items[contactSearchResultLB.SelectedIndex];

            // The contact we found, may already be in our contact list..
            if (contact.contact.IsMemberOfHardwiredGroup(SktContactGroup.TYPE.ALL_BUDDIES))
            {
                MessageBox.Show(this, Global.LangManager.Translate("youAlreadyHaveThisContactAdded") + ": " + contact.contact.P_DISPLAYNAME, Global.LangManager.Translate("alreadyInContactList"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (contact.contact.P_SKYPENAME == Global.OpenForms.mainForm.skype.GetAccountInstance().P_SKYPENAME)
            {
                MessageBox.Show(this, Global.LangManager.Translate("doYouReallyWantAddYourself"), Global.LangManager.Translate("doYouReallyWantAddYourself"), MessageBoxButtons.OK, MessageBoxIcon.Question);
                return;
            }

            Global.OpenForms.mainForm.Invoke(new MethodInvoker(() =>
            {
                string authText = Global.LangManager.Translate("goodDay") + " " + contact.contact.P_DISPLAYNAME + ". " + Global.LangManager.Translate("iWouldLikeAddYouOnSkype") + Global.OpenForms.mainForm.skype.GetAccountInstance().P_FULLNAME;
                if (ShowDialogs.InputDialog.Show(Global.LangManager.Translate("newSkypeContact"), Global.LangManager.Translate("youCanAddYourTextToRequest"), ref authText) == System.Windows.Forms.DialogResult.OK)
                {
                    if (Global.OpenForms.mainForm.skype.requestContactAuth(contact.contact.P_SKYPENAME, authText) == true)
                        MessageBox.Show(Global.LangManager.Translate("authRequestWasSent"), Global.LangManager.Translate("authRequestWasSent"), MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    else
                        MessageBox.Show(Global.LangManager.Translate("authRequestSendingFailed"), Global.LangManager.Translate("authRequestSendingFailed"), MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                }
            }));
        }

        private void quickAddButton_Click(object sender, EventArgs e)
        {
            string skypename = skypeNameTextBox.Text;

            Global.OpenForms.mainForm.Invoke(new MethodInvoker(() =>
            {
                string authText = Global.LangManager.Translate("goodDay") + ". " + Global.LangManager.Translate("iWouldLikeAddYouOnSkype") + Global.OpenForms.mainForm.skype.GetAccountInstance().P_FULLNAME;
                if (ShowDialogs.InputDialog.Show(Global.LangManager.Translate("newSkypeContact"), Global.LangManager.Translate("enterSkypeName"), ref skypename, ref authText) == System.Windows.Forms.DialogResult.OK)
                {
                    if (Global.OpenForms.mainForm.skype.requestContactAuth(skypename, authText) == true)
                        MessageBox.Show(Global.LangManager.Translate("authRequestWasSent"), Global.LangManager.Translate("authRequestWasSent"), MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    else
                        MessageBox.Show(Global.LangManager.Translate("authRequestSendingFailed"), Global.LangManager.Translate("authRequestSendingFailed"), MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                }
            }));
        }
    }
}
